package com.xuelang.pipeline.entry.pipeline;

import com.xuelang.pipeline.app.pipeline.PipelineService;
import com.xuelang.pipeline.client.common.constant.Constant;
import com.xuelang.pipeline.client.common.constant.DictDTO;
import com.xuelang.pipeline.client.common.request.PageParam;
import com.xuelang.pipeline.client.common.result.ApiResult;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.client.common.vo.SelectVO;
import com.xuelang.pipeline.client.pipeline.IPipelineService;
import com.xuelang.pipeline.client.pipeline.dto.*;
import com.xuelang.pipeline.client.pipeline.enums.PipelineInstanceEnum;
import com.xuelang.pipeline.client.pipeline.enums.TriggerWayEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 流水线服务web控制类
 *
 * @author xiwen.zxg
 * @since 2024/5/22
 */
@Slf4j
@RestController
@RequestMapping("/pipeline")
public class PipelineController implements IPipelineService {

    @Autowired
    private PipelineService pipelineService;

    @Override
    public ApiResult<PageRecord<PipelineDTO>> pagePipelineList(PageParam<PipelineQuery> dtoPageParam) {
        return ApiResult.success(pipelineService.pageList(dtoPageParam));
    }

    @Override
    public ApiResult<PageRecord<MyPipelineInfoDTO>> pageMyPipelineList(PageParam<MyPipelineQuery> dtoPageParam) {
        return ApiResult.success(pipelineService.pageMyPipelineList(dtoPageParam));
    }

    @Override
    public ApiResult<List<PipelineDTO>> conditionalPipelineList(PipelineQuery pipelineQuery) {
        return ApiResult.success(pipelineService.list(pipelineQuery));
    }

    @Override
    public ApiResult<Boolean> addPipeline(PipelineConfigDTO pipelineConfigDTO) {
        return ApiResult.success(pipelineService.add(pipelineConfigDTO));
    }

    @Override
    public ApiResult<Boolean> updatePipeline(PipelineConfigDTO pipelineConfigDTO) {
        return ApiResult.success(pipelineService.update(pipelineConfigDTO));
    }

    @Override
    public ApiResult<Boolean> deletePipeline(String code) {
        return ApiResult.success(pipelineService.delete(code));
    }

    @Override
    public ApiResult<PipelineDTO> queryPipeline(String code) {
        return ApiResult.success(pipelineService.query(code));
    }

    @Override
    public ApiResult<ComponentPipelineDTO> queryComponentPipeline(HttpServletRequest request, ComponentPipelineQuery pipelineQuery) {
        String projectCode = request.getHeader(Constant.PROJECT_CODE_HEADER_KEY);
        pipelineQuery.setProjectCode(projectCode);
        return ApiResult.success(pipelineService.componentPipeline(pipelineQuery));
    }

    @Override
    public ApiResult<PipelineConfigDTO> queryPipelineConfig(String code) {
        return ApiResult.success(pipelineService.pipelineConfig(code));
    }

    @Override
    public ApiResult<String> queryPipelineWebhook(Long pipelineId, String preUri) {
        return ApiResult.success(preUri + "/webhook/" + pipelineId);
    }


    @Override
    public ApiResult<List<DictDTO>> queryDeployEnvDict(DeployCompTaskQuery deployCompTaskQuery) {
        return  ApiResult.success(pipelineService.queryDeployEnvDict(deployCompTaskQuery));
    }

    @Override
    public ApiResult<String> queryDeployYaml(String code) {
        return ApiResult.success(pipelineService.queryYamlConfig(code));
    }

    @Override
    public void downloadTaskLog(String envId, String podName, String containerName, HttpServletResponse response) {
        pipelineService.downloadTaskLog(envId, podName, containerName, response);
    }

    @Override
    public ApiResult<List<String>> findPipelineNotifyReceiverList(String code) {
        return ApiResult.success(pipelineService.findPipelineNotifyReceiverList(code));
    }

    @Override
    public ApiResult<List<SelectVO>> listTriggerWay() {
        List<SelectVO> selectVOList = Arrays.stream(TriggerWayEnum.values())
                .map(triggerWayEnum -> SelectVO.builder()
                        .id(triggerWayEnum.getValue().toString())
                        .text(triggerWayEnum.getDesc())
                        .build())
                .collect(Collectors.toList());
        return ApiResult.success(selectVOList);
    }

    @Override
    public ApiResult<List<SelectVO>> listPipelineInstanceStatus() {
        List<SelectVO> selectVOList = Arrays.stream(PipelineInstanceEnum.values())
                .filter(triggerWayEnum -> triggerWayEnum != PipelineInstanceEnum.notStart) // 过滤掉 notStart
                .map(triggerWayEnum -> SelectVO.builder()
                        .id(triggerWayEnum.getKey().toString())
                        .text(triggerWayEnum.getValue())
                        .build())
                .collect(Collectors.toList());
        return ApiResult.success(selectVOList);
    }


}
